﻿#region "Copyright (C) Lenny Granovsky. 2011-2013"
//    This library provides extended cache capabilities to the ASP.NET applications.
//
//                Copyright (C) Lenny Granovsky. 2011-2013. 
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 2 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
//    This program comes with ABSOLUTELY NO WARRANTY.
//    This is free software, and you are welcome to redistribute it
//    under certain conditions;
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Caching;

namespace FlexiCache
{
    /// <summary>
    /// Class provides functionality for Output Cache. It implements the OutputCacheProvider class. 
    /// It uses the MongoDBCacheProviderBase as an underlying storage connector. 
    /// </summary>
    /// <remarks>This class is not intended to be used directly. It used internally by ASP.NET infrastructure.</remarks>
    public class ExternalOutputCacheProvider : ExternalCacheProviderBase, IDisposable
    {
        /// <summary>
        /// Default constructor.
        /// </summary>
        public ExternalOutputCacheProvider()
            : base(CachingConfiguration.Current.CustomOutputCacheConfig)
        {
        }

        /// <summary>
        /// Default constructor.
        /// </summary>
        public ExternalOutputCacheProvider(bool useCompression)
            : base(CachingConfiguration.Current.CustomOutputCacheConfig)
        {
            this.CacheStorageAdapter.UseCompression = useCompression;
        }

        /// <summary>
        /// Method adds data into the cache with specified key and expiration.
        /// </summary>
        /// <param name="key">Data key.</param>
        /// <param name="entry">Data to cache.</param>
        /// <param name="utcExpiry">Expiration time (UTC).</param>
        /// <returns>Returns cached object.</returns>
        public override object Add(string key, object entry, DateTime utcExpiry)
        {
            if (CachingConfiguration.Current.CustomOutputCacheConfig.IsEnabled)
            {
                //if collecting of metadata is enabled - auto-collect page params that have reference in the ASP.NET Output Cache key.
                string[] metaTags = (CachingConfiguration.Current.EnableMetatagsCollection) 
                    ? CachedResourceFinder.CreateMetaTagsCollection(key, System.Web.HttpContext.Current) 
                    : null;
                return base.Add(key, entry, utcExpiry, "", metaTags);
            }
            return entry;
        }

        /// <summary>
        /// Method gets a data from the cache using specified key.
        /// </summary>
        /// <param name="key">Data key.</param>
        /// <returns>Returns cached object.</returns>
        public override object Get(string key)
        {
            if (CachingConfiguration.Current.CustomOutputCacheConfig.IsEnabled)
                return base.Get(key);
            return null;
        }

        /// <summary>
        /// Method removes data from the cache using specified key.
        /// </summary>
        /// <param name="key">Data key.</param>
        public override void Remove(string key)
        {
            if (CachingConfiguration.Current.CustomOutputCacheConfig.IsEnabled)
                base.Remove(key);
        }

        /// <summary>
        /// Method sets (updates or adds) data into the cache with specified key and expiration.
        /// </summary>
        /// <param name="key">Data key.</param>
        /// <param name="entry">Data to cache.</param>
        /// <param name="utcExpiry">Expiration time (UTC).</param>
        public override void Set(string key, object entry, DateTime utcExpiry)
        {
            if (CachingConfiguration.Current.CustomOutputCacheConfig.IsEnabled)
            {
                //if collecting of metadata is enabled - auto-collect page params that have reference in the ASP.NET Output Cache key.
                string[] metaTags = (CachingConfiguration.Current.EnableMetatagsCollection) 
                    ? CachedResourceFinder.CreateMetaTagsCollection(key, System.Web.HttpContext.Current) 
                    : null;
                base.Set(key, entry, utcExpiry, "", metaTags);
            }
        }

        /// <summary>
        /// Method removes cached data from the output cache using specified meta tags.
        /// </summary>
        /// <remarks>If all parameters are empty, the the request is ignored.</remarks>
        /// <param name="path">The path used to request the data. Can be empty or null.</param>
        /// <param name="metaTags">The collection of metatags to locate the data.</param>
        public void Remove(string path, string[] metaTags)
        {
            if (CachingConfiguration.Current.CustomOutputCacheConfig.IsEnabled)
            {
                List<string> unionedTags = new List<string>();
                if (metaTags != null)
                    unionedTags.AddRange(metaTags);
                unionedTags.Sort();
                if (!string.IsNullOrWhiteSpace(path))
                    unionedTags.Insert(0, CachedResourceFinder.HandlePathTag(path));
                metaTags = unionedTags.ToArray();
                base.CacheStorageAdapter.Remove(metaTags);
            }
        }
    }
}
